package cn.zt.controller;

import cn.zt.dto.ResultDTO;
import cn.zt.model.FileModel;
import cn.zt.model.RotaionPicture;
import cn.zt.service.FileModelService;
import cn.zt.util.GuidUtil;
import cn.zt.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @Author: zt
 * @Date: 2020/6/9 11:25
 */
@RestController
@RequestMapping("/file")
@Configuration
@Api(value = "file", description = "文件上传接口")
public class FileController {

    @Autowired
    private FileModelService fileModelService;

    @Value(value = "${application.profile}")
    private String profile;


    @GetMapping(value = "/rotationPicture")
    @ApiOperation("获取轮播图接口")
    public ResultDTO rotationPicture() {

        List<RotaionPicture> pictureList = new ArrayList<>();
        try {
            pictureList = fileModelService.queryRotationPicture();
            for (RotaionPicture rotaionPicture : pictureList) {
                rotaionPicture.setImgSrc(rotaionPicture.getImgPath()+rotaionPicture.getImgSrc());
                rotaionPicture.setLink("");
            }
        } catch (Exception e) {
            return ResultDTO.isFail(e);
        }
        return ResultDTO.isOk().data(pictureList);
    }

    @PostMapping(value = "/fileUpload")
    @ApiOperation("文件上传")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = profile; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "/temp-file/" + fileName;
        model.addAttribute("filename", filename);
        //保存图片路径和文件名到数据库
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            FileModel fileModel = new FileModel();
            fileModel.setId(GuidUtil.getGuid());
            fileModel.setFileName(fileName);
            fileModel.setFileTime(sdf.format(new Date()));
            fileModel.setFilePath("D://temp-file//");
            fileModelService.saveFile(fileModel);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "file";
    }
}
